
<h1><span class="yiyi-st" id="yiyi-12">numpy.bincount</span></h1>
        <blockquote>
        <p>原文：<a href="https://docs.scipy.org/doc/numpy/reference/generated/numpy.bincount.html">https://docs.scipy.org/doc/numpy/reference/generated/numpy.bincount.html</a></p>
        <p>译者：<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
        <p>校对：（虚位以待）</p>
        </blockquote>
    
<dl class="function">
<dt id="numpy.bincount"><span class="yiyi-st" id="yiyi-13"> <code class="descclassname">numpy.</code><code class="descname">bincount</code><span class="sig-paren">(</span><em>x</em>, <em>weights=None</em>, <em>minlength=None</em><span class="sig-paren">)</span></span></dt>
<dd><p><span class="yiyi-st" id="yiyi-14">计算非负整数数组中每个值的出现次数。</span></p>
<p><span class="yiyi-st" id="yiyi-15">块（大小为1）的数量大于<em class="xref py py-obj">x</em>中的最大值。</span><span class="yiyi-st" id="yiyi-16">如果指定<em class="xref py py-obj">minlength</em>，那么在输出数组中至少会有这个数目的bin（如果需要，根据<em class="xref py py-obj">x</em>的内容，它会更长）。</span><span class="yiyi-st" id="yiyi-17">每个bin给出其索引值在<em class="xref py py-obj">x</em>中的出现次数。</span><span class="yiyi-st" id="yiyi-18">If <em class="xref py py-obj">weights</em> is specified the input array is weighted by it, i.e. if a value <code class="docutils literal"><span class="pre">n</span></code> is found at position <code class="docutils literal"><span class="pre">i</span></code>, <code class="docutils literal"><span class="pre">out[n]</span> <span class="pre">+=</span> <span class="pre">weight[i]</span></code> instead of <code class="docutils literal"><span class="pre">out[n]</span> <span class="pre">+=</span> <span class="pre">1</span></code>.</span></p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name">
<col class="field-body">
<tbody valign="top">
<tr class="field-odd field"><th class="field-name"><span class="yiyi-st" id="yiyi-19">参数：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-20"><strong>x</strong>：array_like，1 dimension，nonnegative ints</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-21">输入数组。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-22"><strong>权重</strong>：array_like，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-23">权重，与<em class="xref py py-obj">x</em>形状相同的数组。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-24"><strong>minlength</strong>：int，可选</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-25">输出数组的最小数目。</span></p>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-26"><span class="versionmodified">版本1.6.0中的新功能。</span></span></p>
</div>
</div></blockquote>
</td>
</tr>
<tr class="field-even field"><th class="field-name"><span class="yiyi-st" id="yiyi-27">返回：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-28"><strong>out</strong>：intar的ndarray</span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-29">binning输入数组的结果。</span><span class="yiyi-st" id="yiyi-30"><em class="xref py py-obj">out</em>的长度等于<code class="docutils literal"><span class="pre">np.amax(x)+1</span></code>。</span></p>
</div></blockquote>
</td>
</tr>
<tr class="field-odd field"><th class="field-name"><span class="yiyi-st" id="yiyi-31">上升：</span></th><td class="field-body"><p class="first"><span class="yiyi-st" id="yiyi-32"><strong>ValueError</strong></span></p>
<blockquote>
<div><p><span class="yiyi-st" id="yiyi-33">如果输入不是一维的，或包含负值的元素，或<em class="xref py py-obj">minlength</em>是非正数。</span></p>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-34"><strong>TypeError</strong></span></p>
<blockquote class="last">
<div><p><span class="yiyi-st" id="yiyi-35">如果输入的类型是float或complex。</span></p>
</div></blockquote>
</td>
</tr>
</tbody>
</table>
<div class="admonition seealso">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-36">也可以看看</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-37"><a class="reference internal" href="numpy.histogram.html#numpy.histogram" title="numpy.histogram"><code class="xref py py-obj docutils literal"><span class="pre">histogram</span></code></a>，<a class="reference internal" href="numpy.digitize.html#numpy.digitize" title="numpy.digitize"><code class="xref py py-obj docutils literal"><span class="pre">digitize</span></code></a>，<a class="reference internal" href="numpy.unique.html#numpy.unique" title="numpy.unique"><code class="xref py py-obj docutils literal"><span class="pre">unique</span></code></a></span></p>
</div>
<p class="rubric"><span class="yiyi-st" id="yiyi-38">例子</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">bincount</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">5</span><span class="p">))</span>
<span class="go">array([1, 1, 1, 1, 1])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">bincount</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">7</span><span class="p">]))</span>
<span class="go">array([1, 3, 1, 1, 0, 0, 0, 1])</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">23</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">bincount</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">.</span><span class="n">size</span> <span class="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">amax</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span>
<span class="go">True</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-39">输入数组需要是整数dtype，否则会引发TypeError：</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">bincount</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float</span><span class="p">))</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">TypeError</span>: <span class="n">array cannot be safely cast to required type</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-40"><code class="docutils literal"><span class="pre">bincount</span></code>的一种可能用法是使用<code class="docutils literal"><span class="pre">weights</span></code>关键字对数组的可变大小块执行求和。</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">w</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">0.3</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">,</span> <span class="mf">0.7</span><span class="p">,</span> <span class="mf">1.</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.6</span><span class="p">])</span> <span class="c1"># weights</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">bincount</span><span class="p">(</span><span class="n">x</span><span class="p">,</span>  <span class="n">weights</span><span class="o">=</span><span class="n">w</span><span class="p">)</span>
<span class="go">array([ 0.3,  0.7,  1.1])</span>
</pre></div>
</div>
</dd></dl>
